/*
 * Powered By [rapid-framework]
 * Web Site: http://www.rapid-framework.org.cn
 * Google Code: http://code.google.com/p/rapid-framework/
 * Since 2008 - 2010
 */

package com.sisterandbrother.action;

import java.util.Date;
import java.util.List;

import javacommon.base.BaseStruts2Action;
import javacommon.util.ChatUtils;
import javacommon.util.IpHelper;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import cn.org.rapid_framework.page.Page;

import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;
import com.sisterandbrother.model.ChatMsg;
import com.sisterandbrother.query.ChatMsgQuery;
import com.sisterandbrother.service.ChatMsgManager;

@SuppressWarnings({ "rawtypes", })
public class ChatAction extends BaseStruts2Action implements Preparable, ModelDriven {

	private static final long serialVersionUID = 2473523204031348906L;

	protected Log log = LogFactory.getLog(getClass());

	protected static final String DEFAULT_SORT_COLUMNS = "creationTime desc";

	protected static final String LIST_JSP = "/WEB-INF/pages/ChatMsg/list.jsp";

	private ChatMsgManager chatMsgManager;

	Long id = null;

	private ChatMsg chatMsg;

	public ChatMsg getChatMsg() {
		return chatMsg;
	}

	public ChatMsgManager getChatMsgManager() {
		return chatMsgManager;
	}

	public Long getId() {
		return id;
	}

	public Object getModel() {
		return chatMsg;
	}

	public String list() {
		ChatMsgQuery query = newQuery(ChatMsgQuery.class, DEFAULT_SORT_COLUMNS);
		Page page = chatMsgManager.findPage(query);
		savePage(page, query);
		return LIST_JSP;
	}

	public void prepare() throws Exception {
		if (isNullOrEmptyString(id)) {
			chatMsg = new ChatMsg();
		} else {
			chatMsg = (ChatMsg) chatMsgManager.getById(id);
		}
	}

	public String save() {
		System.out.println("save start");
		chatMsg.setFontColor(ChatUtils.getRandomColor());
		chatMsg.setSenderIp(IpHelper.getIpAddr(getRequest()));
		chatMsg.setSendTime(new Date());
		chatMsgManager.save(chatMsg);
		response("success");
		return null;
	}

	public String getMsg() throws Exception {
		ChatMsgQuery query = new ChatMsgQuery();
		query.setMinId(Long.valueOf(getRequestParameter("currentId")));
		List<ChatMsg> msgs = chatMsgManager.findByQuery(query);
		JSONArray result = JSONArray.fromObject("[]");
		if (msgs != null) {
			for (ChatMsg msg : msgs) {
				JSONObject jsonMsg = JSONObject.fromObject("{}");
				jsonMsg.put("id", msg.getId());
				jsonMsg.put("sendTime", msg.getSendTime().toString());
				jsonMsg.put("receiverName", msg.getReceiverName());
				jsonMsg.put("content", msg.getContent());
				result.add(jsonMsg);
			}
		}
		responseJson(result.toString());
		return null;
	}
	

	public void setChatMsg(ChatMsg chatMsg) {
		this.chatMsg = chatMsg;
	}

	public void setChatMsgManager(ChatMsgManager chatMsgManager) {
		this.chatMsgManager = chatMsgManager;
	}

	public void setId(Long val) {
		this.id = val;
	}

}